/* -+- file  ./kf2/GUI/RosterWindow.cs -+- */
/*
 * kf2 jabber client
 * ~~~~~~~~~~~~~~~~~
 *
 * Copyright (C) 2006-2007 Przemysław Sitek
 * 				<przemyslaw.sitek at wp.pl> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

using System;
using Habazie.kf2.Roster;
using Habazie.kf2.Core;
using Habazie.kf2.Net;

namespace Habazie.kf2.GUI
{
	///<summary>Main application window</summary>
	public class RosterWindow : Gtk.Window
	{
		private App m_App;
		protected Gtk.Notebook m_Notebook;
		protected Gtk.ScrolledWindow m_ScrolledWindow;
		public RosterView m_RV;
		protected WelcomePage wp = null;
		protected Gtk.HPaned m_MainPaned;
		private TabContainer m_Tabs = null;
		private NotifyIcon m_icon;
		private StatusButton m_sb;
				
		public RosterWindow() : 
				base("")
		{
			Stetic.Gui.Build(this, typeof(Habazie.kf2.GUI.RosterWindow));
			this.Title = Util.APPNAME;
			m_RV = new RosterView ();
			
			/* Remove this *unfriendly* viewport */
			if (m_ScrolledWindow.Child != null)
				m_ScrolledWindow.Remove (m_ScrolledWindow.Child);
				
			m_ScrolledWindow.Add (m_RV);
			m_Notebook.SetTabLabel (m_ScrolledWindow, new Gtk.Label ("Contacts"));
			m_RV.Show ();
			
			try {
				wp = new WelcomePage ();
				m_Notebook.PrependPage (wp, new Gtk.Label ("Home"));
				wp.Show ();
			} catch {
				Console.Error.WriteLine (" * Mozilla libraries cannot be found.");
				Console.Error.WriteLine (" * Modify your LD_LIBRARY_PATH environment variable");
				Console.Error.WriteLine (" * so that it points to directory that gtkembedmoz.so");
				Console.Error.WriteLine (" * is contained in.");
//				Console.Error.WriteLine ("Ex: {0}", e.Message);
			}
			
			bool show_tabs = (m_Notebook.NPages > 1);
			m_Notebook.ShowBorder = show_tabs;
			m_Notebook.ShowTabs = show_tabs;
		}
		
		public RosterWindow (App app) : this ()
		{
			m_App = app;
//			Habazie.kf2.Roster.Roster roster = app.xGetRoster ();
//			roster.RosterUpdated += xUpdateRoster;
			
			m_RV.Connections = m_App.Connections;
			m_RV.UpdateViewSingle ();
			m_RV.HeadersVisible = false;
			
			if (wp != null) {
				wp.SetEventBox (m_App.Connections.GetEventBox ());
			}
			
			/* add new sexy status selector */
			m_sb = new StatusButton ();
			Gtk.HBox hbox602 = new Gtk.HBox (false, 6);
			hbox602.PackStart (m_sb, false, false, 0);
			m_sb.Show ();
			
			/* Sexy event notificator */
			EventButton eb = new EventButton (m_App.Connections.GetEventBox ());
			eb.Show ();
			hbox602.PackEnd (eb, false, false, 0);
			hbox602.Show ();
			m_sb.StatusActivated += OnStatusActivated;

			((Gtk.VBox)this.Child).PackEnd (hbox602, false, false, 0);
			
			/* Sexy tabbed interface */
			m_Tabs = new TabContainer (true);
			m_Tabs.TabPos = Gtk.PositionType.Top;
			m_Tabs.AutoHideTabs = false;
			m_MainPaned.Add2 (m_Tabs);
			
			/* Jazzy notification icon */
			m_icon = new NotifyIcon (m_App.Connections.GetEventBox ());
			
			if (m_App.Connections.First != null)
				m_App.Connections.First.Changed += OnStatusChanged;
		}
		
		public TabContainer Tabs {
			get { return m_Tabs; }
		}
		
		
		protected virtual void OnStatusChanged (object sender, System.EventArgs e)
		{
			Account acc = sender as Account;
			m_sb.SetStatus (acc.StatusType, acc.StatusShow);
		}
		

		#region Actions
		protected virtual void OnAwayActivated(object sender, System.EventArgs e)
		{
			StatusDialog win = new StatusDialog (m_App, jabber.protocol.client.PresenceType.available, "away");
			win.Show ();
		}

		protected virtual void OnChatWithMeActivated(object sender, System.EventArgs e)
		{
			StatusDialog win = new StatusDialog (m_App, jabber.protocol.client.PresenceType.available, "chat");
			win.Show ();
		}

		protected virtual void OnDoNotDisturbActivated(object sender, System.EventArgs e)
		{
			StatusDialog win = new StatusDialog (m_App, jabber.protocol.client.PresenceType.available, "dnd");
			win.Show ();
		}

		protected virtual void OnExtendedAwayActivated(object sender, System.EventArgs e)
		{
			StatusDialog win = new StatusDialog (m_App, jabber.protocol.client.PresenceType.available, "xa");
			win.Show ();
		}

		protected virtual void OnOfflineActivated(object sender, System.EventArgs e)
		{
		}

		protected virtual void OnOnlineActivated(object sender, System.EventArgs e)
		{
			StatusDialog win = new StatusDialog (m_App, jabber.protocol.client.PresenceType.available, null);
			win.Show ();
		}

		protected virtual void OnQuitActivated(object sender, System.EventArgs e)
		{
			Gtk.Application.Quit ();
		}

		protected virtual void OnMessageActivated(object sender, System.EventArgs e)
		{
			NewMessage win = new NewMessage ();
			win.Show ();
		}

		protected virtual void OnNewContactActivated(object sender, System.EventArgs e)
		{
			AddContactDialog win = new AddContactDialog (m_App);
			win.Show ();
		}

		protected virtual void OnAccountSettingsActivated(object sender, System.EventArgs e)
		{
			AccountListDialog win = new AccountListDialog (m_App);
			win.Show ();
		}

		protected virtual void OnPreferencesActivated(object sender, System.EventArgs e)
		{
			SettingsDialog win = new SettingsDialog (m_App);
			win.Show ();
		}

		/* Show about dialog */
		protected virtual void OnDialogInfoActivated(object sender, System.EventArgs e)
		{
			Gtk.AboutDialog win = new Gtk.AboutDialog ();
			win.Authors = new string [] {"Przemysław Sitek"};
			win.Comments = "Simple XMPP/Jabber messenger";
			win.Copyright = "(c) 2006 Przemysław Sitek";
			win.License = "GNU GPL";
			win.Name = Util.APPNAME;
			win.Version = Util.VERSION;
			win.Show ();
			win.Run ();
			win.Destroy ();
		}
		
		
		protected virtual void OnStatusActivated (object o, StatusEventArgs args)
		{
			StatusDialog win = new StatusDialog (m_App, args.Presence, args.Show);
			win.Show ();
		}
		

		protected virtual void OnOfflineContactsToggled(object sender, System.EventArgs e)
		{
			Gtk.ToggleAction toggle = (Gtk.ToggleAction) sender;
			m_RV.DisplayOffline = toggle.Active;
		}

		protected virtual void OnDeleteEvent (object o, Gtk.DeleteEventArgs args)
		{
			Gtk.Application.Quit ();
		}
		#endregion
	}
	
}
